import axios from 'axios';

// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};
// 退出请求方法
const logoutreq = (method, url) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    }).then(res => res.data);
};

// 通用公用方法(测试不行)
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token: localStorage.getItem('logintoken')
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};
//get请求不带参数
const requestQuickGet = (url) => {
    return new Promise((resolve, reject) => {
      axios.get(url).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
};
//get请求
const requestGet = (url, params) => {
    return new Promise((resolve, reject) => {
      let params_v = {
        params:params
      }
      axios.get(url, params_v).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
};
//post请求
const requestPost = (url, params) => {
    return new Promise((resolve, reject) => {
        axios.post(url, params).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
};
// post请求From
const requestPostForm =  (url, params) => {
    return new Promise((resolve, reject) => {
      axios.post(url, params, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      }).then(res => {
        resolve(res.data)//注意res是axios封装的对象，res.data才是服务端返回的信息
      }).catch(error => {
        reject(error)
      })
    })
};
//put请求
const requestPut = (url, params) => {
    return new Promise((resolve, reject) => {
      axios.put(url, params).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
};
  //delete请求
const requestDelete =  (url, params) => {
    return new Promise((resolve, reject) => {
        axios.delete(url, params).then(res => {
        resolve(res.data)
        }).catch(error => {
        reject(error)
        })
    })
};

export {
    loginreq,
    logoutreq,
    requestQuickGet,
    requestGet,
    req,
    requestPut,
    requestDelete
}